package com.year_23.may;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: jsh
 * @create: 2023/5/7 22:19
 * @Description:    1010 ：总持续时间可被60整除的歌曲对
 *                  https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/
 * @FileName: Day07
 * @History:
 * @自定义内容：
 */
public class Day07 {

    public static void main(String[] args) {
        int[] times = {60,60,60};
//        int[] times = {30,20,150,100,40};
        System.out.println(numPairsDivisibleBy60(times));
        System.out.println(numPairsDivisibleBy60_2(times));

    }

    public static int numPairsDivisibleBy60_2(int[] time){
        int res = 0;
        int[] arr = new int[60];
        for (int i : time){
            arr[i % 60] ++;
        }

        for (int i = 1; i < 30; i++) {
            res += arr[i] * arr[60 - i];
        }
        res += arr[0] * (arr[0] - 1) /2 + arr[30] * (arr[30] -1)/2;

        return res;
    }









    // 可将map 变成 int[]数组
    public static int numPairsDivisibleBy60(int[] times){
        int res = 0;
        Map<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < times.length; i++) {
            int temp = times[i] % 60;
            if (map.containsKey((60 - temp) % 60)) {
                res += map.get((60 - temp) % 60);
            }
            if ( !map.containsKey(temp)){
                map.put(temp, 1);
            }else {
                map.put(temp,map.get(temp) + 1);
            }

        }
        return res;
    }
}
